package cn.dt70.controller;

import cn.dt70.pojo.AqbInterest;
import cn.dt70.pojo.AqbInterestandcode;
import cn.dt70.pojo.AqbUser;
import cn.dt70.service.AqbInterestService;
import cn.dt70.service.AqbInterestandcodeService;
import cn.dt70.utils.Constants;
import cn.dt70.utils.PageSupport;
import com.alibaba.fastjson.JSON;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/zyf/")
public class Controllerzyf {


    @Resource
    private AqbInterestService aqbInterestService;

    @Resource
    private AqbInterestandcodeService aqbInterestandcodeService;

    @RequestMapping("show")
    public String show() {
        System.out.println("----进入控制器！----");
        return "usercenter-rewardcontrol-interestcoupon";
    }

    @RequestMapping("showJxj")
    @ResponseBody
    public String showJxj(@Param("status") String status, PageSupport ps, HttpSession session,
                          @Param("currentPageNo") String currentPageNo) {
        if (null == status || "".equals(status)) {
            status = "0";
        }
        if (null == currentPageNo || "".equals(currentPageNo)) {
            ps.setCurrentPageNo(0);
        } else {
            ps.setCurrentPageNo(Integer.parseInt(currentPageNo));
        }
        AqbUser aqbUser = (AqbUser) session.getAttribute(Constants.USER_SESSION);
        List<AqbInterest> aqbInterestList = aqbInterestService.getAllByUid(aqbUser.getUserId(), Integer.valueOf(status) - 1, ps);
        String aqbInterests = JSON.toJSONString(aqbInterestList);
        String phs = JSON.toJSONString(ps);
        StringBuffer sb = new StringBuffer("{");
        sb.append("\"aqbInterests\":" + aqbInterests + "");
        sb.append(",\"phs\":" + phs + "");
        sb.append(",\"status\":" + status + "");
        sb.append("}");
        return sb.toString();
    }

    @RequestMapping("conversion")
    @ResponseBody
    public String conversion(@Param("rewardcode") String rewardcode, HttpSession session) {
        AqbUser aqbUser = (AqbUser) session.getAttribute(Constants.USER_SESSION);
        AqbInterestandcode aqbInterestandcode = aqbInterestandcodeService.getJxj(rewardcode);
        if(aqbInterestService.addInterest(aqbUser.getUserId(),aqbInterestandcode)){
            aqbInterestandcodeService.updateStatus(rewardcode);
        }
        return "d";
    }
}
